package com.task.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.task.bean.Rank;
import com.task.bean.Student;
import com.task.service.IRankService;
import com.task.service.IStudentService;
import com.task.vo.ResultVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liuzhe
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IRankService rankService;

    /**
     * 根据老师ID查询其下的学生
     *
     * @param teaId
     * @return
     */
    @GetMapping("/list")
    public ResultVo list(int teaId) {
        return ResultVo.success(studentService.queryStuByTeaId(teaId));
    }

    @GetMapping("/queryStu")
    public ResultVo queryStu(int pageNum, int pageSize) {
        return ResultVo.success(studentService.queryStuPage(pageNum, pageSize));
    }

    @GetMapping("/queryStuNo")
    public ResultVo queryStuNo(int teaId) {
        return ResultVo.success(studentService.queryStuNo(teaId));
    }

    @PostMapping("/login")
    public ResultVo login(@RequestBody Student student) {
        Student result = studentService.queryStuByUsernameAndPassword(student);
        if (ObjectUtils.isNotEmpty(result)) {
            return ResultVo.success(result);
        } else {
            return ResultVo.failed("登录失败");
        }
    }

    @GetMapping("/getAll")
    public ResultVo getAll() {
        return ResultVo.success(studentService.list());
    }

    @GetMapping("/getAllTeacher")
    public ResultVo getAllTeacher(int stuId) {
        return ResultVo.success(studentService.queryTeaByStuId(stuId));
    }

    @DeleteMapping("/delete")
    @Transactional
    public ResultVo delete(int id) {
        rankService.remove(new QueryWrapper<Rank>().eq("stu_id",id));
        return ResultVo.success(studentService.removeById(id));
    }

    @PostMapping("/add")
    public ResultVo add(@RequestBody Student student) {
        return ResultVo.success(studentService.save(student));
    }
}

